package com.lz.autotest.common.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.core.Converter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ModelConvertUtil {
    private static final Logger log = LoggerFactory.getLogger(ModelConvertUtil.class);
    private static ConcurrentMap<String, BeanCopier> beanCopiers = new ConcurrentHashMap();

    public ModelConvertUtil() {
    }

    public static <A, B> B copyTo(A obj, Class<B> clz) {
        try {
            if (obj == null) {
                return null;
            } else {
                B clone = clz.newInstance();
                String key = clz.getName() + ":" + obj.getClass().getName();
                BeanCopier copier = null;
                if (beanCopiers.containsKey(key)) {
                    copier = (BeanCopier)beanCopiers.get(key);
                } else {
                    copier = BeanCopier.create(obj.getClass(), clz, false);
                    beanCopiers.putIfAbsent(key, copier);
                }

                copier.copy(obj, clone, (Converter)null);
                return clone;
            }
        } catch (Exception var5) {
            log.error("Copy source {} to single object {} error.", new Object[]{obj.getClass().getName(), clz.getName(), var5});
            throw new RuntimeException(var5);
        }
    }

    public static <A, B> List<B> copyListTo(List<A> sources, Class<B> clz) {
        try {
            if (sources == null) {
                return null;
            } else {
                List<B> newList = Lists.newArrayList();
                if (sources.size() == 0) {
                    return newList;
                } else {
                    A val = sources.get(0);
                    String key = clz.getName() + ":" + val.getClass().getName();
                    BeanCopier copier = null;
                    if (beanCopiers.containsKey(key)) {
                        copier = (BeanCopier)beanCopiers.get(key);
                    } else {
                        copier = BeanCopier.create(val.getClass(), clz, false);
                        beanCopiers.putIfAbsent(key, copier);
                    }

                    Iterator var6 = sources.iterator();

                    while(var6.hasNext()) {
                        A a = (A) var6.next();
                        B clone = clz.newInstance();
                        copier.copy(a, clone, (Converter)null);
                        newList.add(clone);
                    }

                    return newList;
                }
            }
        } catch (Exception var9) {
            log.error("Copy to list object {} error.", clz.getName(), var9);
            throw new RuntimeException(var9);
        }
    }

    public static <A, B> Page<B> copyPageTo(Page<A> source, Class<B> clz) {
        Page<B> result = new Page();
        result.setTotal(source.getTotal());
        result.setCurrent(source.getCurrent());
        result.setSize(source.getSize());
        result.setRecords(copyListTo(source.getRecords(), clz));
        return result;
    }
}
